<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="emp" value="${sessionScope.user}"/>
<c:forEach var="ro" items="${emp.roles}">
	<c:forEach var="fu" items="${ro.functionAll}">
		<c:if test="${fu.code=='document-download'}">
			<c:set var="download" value="true"/>
		</c:if>
		<c:if test="${fu.code=='document-delete'}">
			<c:set var="deleteDoc" value="true"/>
		</c:if>
	</c:forEach>
</c:forEach>

<!-- jQuery 2.0.2 -->
<script src="${initParam.root }/static/js/jquery.min.js"></script>
<script src="${initParam.root }/static/js/jquery-migrate-1.2.1.js"></script>
<!-- Bootstrap -->
<script src="${initParam.root }/static/js/bootstrap.min.js" type="text/javascript"></script>
<!-- <script
	src="${initParam.root }/static/js/plugins/datatables/dataTables.bootstrap.js"
	type="text/javascript"></script> -->
<!-- AdminLTE App -->
<script src="${initParam.root }/static/js/AdminLTE/app.js" type="text/javascript"></script>
<!-- jstree -->
<script src="${initParam.root }/static/jstree/dist/jstree.min.js"></script>
<script src="${initParam.root }/static/datatables/jquery.dataTables.js"></script>
<script src="${initParam.root }/static/datatables/dataTables.bootstrap.js"></script>
<!-- page script -->
<script type="text/javascript">
	$(function() {
		$("#uploadBtn").click(function(){//上传
			var postData = new FormData($("#uploadForm").get(0));
			$.ajax({
				url:"${initParam.root}/api/document/upload",
				type:"post",
				dataType:"json",
				data: postData,
				contentType: false,
				processData: false,
				success:function(){
					table.ajax.reload();
					$("#ajaxTree").jstree().refresh();
					$("#uploadName").val("");//
					$("#uploadDescription").val("");//
					$("#uploadForm")[0].reset();
				}
			});
		});
		$("#fileTable").delegate(".delete","click",function(){//删除
			var fileId = $(this).attr("data-ref");
			$.ajax({
				url:"${initParam.root}/api/document/delete/"+fileId,
				type:"post",
				success:function(){
					table.ajax.reload();
					$("#ajaxTree").jstree().refresh();
				}
				
			});
		});
		$('#ajaxTree').jstree({//加载树
			'core' : {
				'data' : {
					"url" : "${initParam.root}/api/document/loadTree",
					"dataType" : "json",
					'data' : function(node) {
						return {
							'id' : node.id
						};
					}
				}
			},
			"plugins" : [  
		        "contextmenu"  
		      ],
			"contextmenu":{  
		        "items":{  
		            "create":null,  
		            "rename":null,  
		            "remove":null,  
		            "ccp":null,  
		            "新建文件夹":{  
		                "label":"新建文件夹",  
		                "action":function(data){  
		                    var inst = jQuery.jstree.reference(data.reference),  
		                    obj = inst.get_node(data.reference);
		                    $("#addFolderFormParentId").val(obj.id);//填充到模态框
		                    $("#documentName").val("");//清空
		                    $("#documentDescription").val("");//清空
		                    $("#addFolderPanel").modal("show");//显示模态框
		                }  
		            }
		        }
			}
		}).on("changed.jstree", function(e, data) {//选中树
			if (data.selected.length) {
				$("#paramsp").val(data.instance.get_node(data.selected[0]).id);//加载datatable
				table.ajax.reload();
				$("#uploadParentId").val(data.instance.get_node(data.selected[0]).id);//给上传文件表单的uploadParentId
				$("#localtion").text(data.instance.get_path(data.selected[0],"/",0));//给字符串
				
			}
		});
		//datatable
		var table = $("#fileTable")
				.DataTable(
						{
							ajax : {
								url : "${initParam.root}/api/document/fileByParentId",
								data : function(param) {
									param.parentId = $("#paramsp").val();//根据hidden元素的val()动态发送异步请求
								}
							},
							searching : false,
							language : {
								"lengthMenu" : "_MENU_ 条记录每页",
								"zeroRecords" : "此文件夹没有内容",
								"info" : "第 _PAGE_ 页 ( 总共 _PAGES_ 页 )",
								"infoEmpty" : "无记录",
								"infoFiltered" : "(从 _MAX_ 条记录过滤)",
								"paginate" : {
									"previous" : "上一页",
									"next" : "下一页"
								}
							},
							columns : [ {
								"data" : "name"
							}, {
								"data" : "description"
							}, {
								"data" : "fileUrl"
							}, {
								"data" : "type"
							}, {
								"data" : "creator.name"
							}, {
								"data" : "createTime"
							} ],
							"lengthMenu" : [ 5, 10, 20, 50 ], 
							"columnDefs" : [ {
								"render" : function(data, type, row) {
									/* var line= '<a class="btn btn-primary edit" href="${initParam.root}/api/document/download?fileName='
											+ row.fileUrl
											+ '">下载 </a><button class="btn btn-danger delete" data-ref="'+row.id+'">删除 </button>'; */
									//console.log("${download}")
									var line="";
									if("${download}"=="true"){
										line+='<a class="btn btn-primary edit" href="${initParam.root}/api/document/download?fileName='
												+row.fileUrl
												+'">下载 </a>'
									}
									if("${deleteDoc}"=="true"){
										line+='<button class="btn btn-danger delete" data-ref="'+row.id+'">删除 </button>';
									}
									if(line==""){
										line="无";
									}	
									return line;		
								},
								"targets" : 6
							} ]
						});
		$("#btnAdd").click(function(){
			var formData = new FormData($("#addFolderForm").get(0));
			$.ajax({
				url:"${initParam.root}/api/document/addFolder",
				type:"post",
				dataType:"json",
				data: formData,
				contentType: false,
				processData: false,
				success:function(){
					table.ajax.reload();
					$("#ajaxTree").jstree().refresh();
					$("#addFolderPanel").modal("hide");//隐藏模态框
					//打开tree的节点
					
				}
			});
		});
	});
</script>
